'use strict';
import { findRoles, createRole, deleteRole, updateRole } from '../services/role';

export default {
  namespace: 'role',
  state: {},
  effects: {
    * findRoles({ payload }, { call, put }) {
      const response = yield call(findRoles, payload);
      if (response) {
        yield put({
          type: 'onDataLoad',
          payload: {
            roles: response.result,
          },
        });
      }
      return response;
    },
    * createRole({ payload }, { call, put }) {
      const response = yield call(createRole, payload);
      return response;
    },
    * deleteRole({ payload }, { call, put }) {
      const response = yield call(deleteRole, payload);
      return response;
    },
    * updateRole({ payload }, { call, put }) {
      const response = yield call(updateRole, payload);
      return response;
    },
  },

  reducers: {
    onDataLoad(state, { payload }) {
      return { ...state, ...payload };
    },
  },
};
